/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.passbookviet.ultilities;

import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sea
 */
public class JSFUtilities {

    /**
     * Get Object in <b>Session</b> using
     * <code>FacesContext </code>
     */
    public static Object getSessionValue(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(key);
    }

    /**
     * Get Object in <b>Session</b> by using
     * <code>HttpServletRequest</code>
     */
    public static Object getSessionValue(HttpServletRequest request, String key) {
        return request.getSession().getAttribute(key);
    }

    /**
     * Set Object in <b>Session</b> using
     * <code>FacesContext </code>
     */
    public static void setSessionValue(String key, Object value) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(key, value);
    }

    /**
     * Remove Object in <b>Session</b> using
     * <code>FacesContext </code>
     */
    public static Object removeSessionValue(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(key);
    }

    /**
     * Remove Object in <b>Session</b> using
     * <code>HttpServletRequest</code>
     */
    public static void removeSessionValue(HttpServletRequest request, String key) {
        request.getSession().removeAttribute(key);
    }

    /**
     * Get Object in <b>Parameter</b> using
     * <code>FacesContext </code> without using
     * <code>HttpServletRequest </code>
     */
    public static String getParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }

    /**
     * Get Object in <b>Parameter</b> using
     * <code>FacesContext </code> using
     * <code>HttpServletRequest </code>
     */
//    public static String getParameter(String key) {
//        return getParameter((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(), key);
//    }
    /**
     * Get Object in <b>Parameter</b> using
     * <code>HttpServletRequest </code>
     */
    public static String getParameter(HttpServletRequest request, String key) {
        return request.getParameter(key);
    }

    /**
     * Add message, often being used in Ajax
     */
    public static void addMessage(String error, FacesMessage mess) {
        FacesContext.getCurrentInstance().addMessage(error, mess);
    }

    /**
     * Dispatch to
     * <code>Url</code> using <b><code>FacesContext</code></b>
     */
    public static void dispatch(String Url) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().dispatch(Url);
    }

    /**
     * Redirect to
     * <code>Url</code> using <b><code>FacesContext</code></b>
     */
    public static void redirect(String Url) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect(Url);
    }

    /**
     * Redirect to
     * <code>Url</code> using <b><code>HttpServletResponse</code></b>
     */
    public static void redirect(HttpServletRequest request, HttpServletResponse response, String Url) throws ServletException, IOException {
        String cp = request.getContextPath();
        Url = (cp == null || cp.length() <= 1) ? Url : cp + Url;
        System.out.println("Redirect to : " + Url);
        response.sendRedirect(Url);
    }

    /**
     * Redirect to
     * <code>outCome</code> using <b><code>JSF Navigation</code></b>
     */
    public static void redirectByNavigator(String from, String outCome) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, from, outCome + "?faces-redirect=true");
    }

    /**
     * Forward to
     * <code>outCome</code> using <b><code>JSF Navigation</code></b>
     */
    public static void forward(String from, String outCome) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, from, outCome);
    }

    /**
     * Get Attribute in
     * <code>ActionEvent</code> by its
     * <code>keys</code>
     */
    public static Object getActionAttribute(ActionEvent event, String key) {
        return event.getComponent().getAttributes().get(key);
    }

    /**
     * Get bean by
     * <code>name</code>
     */
    public static Object getBean(String name) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, name);
    }

    /**
     * Get Real path of <b>/</b>
     */
    public static String getRealPath() {
        return FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
    }

    /**
     * Get Real path of <b>FilePath</b>
     */
    public static String getAbsoluatePath(String filePath) {
        return FacesContext.getCurrentInstance().getExternalContext().getRealPath(filePath);
    }

    /**
     * Get <b>ServerContextPath</n>
     */
    public static String getServerContextPath() {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
    }
}
